import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/save/login.vue')
  },
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/Index/index.vue'),
    redirect: '/mainIndex',
    children: [
      {
        path: '/mainIndex',
        name: 'mainIndex',
        component: () => import('@/components/Index/MainIndex'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/changePass',
        name: 'changePass',
        component: () => import('@/views/save/changePass'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/user',
        name: 'userlist',
        component: () => import('@/views/permManage/userlist'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/user/addUser',
        name: 'addUser',
        component: () => import('@/components/permManage/user/addUser'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/user/updateUser',
        name: 'updateUser',
        component: () => import('@/components/permManage/user/updateUser'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/role',
        name: 'rolelist',
        component: () => import('@/views/permManage/rolelist'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/role/assinPerm',
        name: 'assinPerm',
        component: () => import('@/components/permManage/role/assinPerm'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/lb/lv',
        name: 'leave',
        component: () => import('@/views/leaveAndbusinesstrip/leave'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/lb/addLeave',
        name: 'addLeave',
        component: () => import('@/components/leaveAndBusinessTrip/addLeave'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/lb/bt',
        name: 'businessTrip',
        component: () => import('@/views/leaveAndbusinesstrip/businessTrip'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/lb/addBt',
        name: 'addBt',
        component: () => import('@/components/leaveAndBusinessTrip/addBt'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/lb/ac',
        name: 'audit',
        component: () => import('@/views/leaveAndbusinesstrip/audit'),
        redirect: '/lb/ac/al',
        children: [
          {
            path: '/lb/ac/al',
            name: 'auditLeave',
            component: () => import('@/components/leaveAndBusinessTrip/auditLB/auditLeave'),
            meta: {
              isIntercept: true
            }
          },
          {
            path: '/lb/ac/ab',
            name: 'auditBt',
            component: () => import('@/components/leaveAndBusinessTrip/auditLB/auditBt'),
            meta: {
              isIntercept: true
            }
          }
        ]
      },
      {
        path: '/ad/in',
        name: 'signIn',
        component: () => import('@/views/attendance/signIn'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/ad/out',
        name: 'signOut',
        component: () => import('@/views/attendance/signOut'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/ar/solo',
        name: 'personSc',
        component: () => import('@/views/searchAdc/personSc'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/ar/all',
        name: 'allSc',
        component: () => import('@/views/searchAdc/allSc'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/personel/dm',
        name: 'departMr',
        component: () => import('@/views/personelManerge/departMr'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/personel/pm',
        name: 'personMr',
        component: () => import('@/views/personelManerge/personMr'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/personel/dm/update',
        name: 'updateDepart',
        component: () => import('@/components/personelManerge/updateDepart'),
        meta: {
          isIntercept: true
        }
      },
      {
        path: '/sm',
        name: 'systemSet',
        component: () => import('@/views/systemSet/systemSet'),
        meta: {
          isIntercept: true
        }
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.isIntercept) {
    // 判断
    if (router.app.$options.store.state.token) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})

export default router
